package com.kymatrix.kycarbon.xsmd.system.server.controller;

import cn.hutool.core.util.StrUtil;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.module.InsertModuleReq;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.module.ListQueryModuleResp;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.module.QueryModuleResp;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.module.SortModuleReq;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.module.UpdateModuleReq;
import com.kymatrix.kycarbon.xsmd.system.server.service.IModuleService;
import com.kymatrix.kycarbon.xsmd.common.annotation.ApiAccessLog;
import com.kymatrix.kycarbon.xsmd.common.business.common.ResultResp;
import com.kymatrix.kycarbon.xsmd.common.util.TreeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@Api(tags = "模块管理")
public class ModuleController {

    @Resource
    IModuleService moduleService;

    @ApiOperation("添加模块")
    @PostMapping("/module/add")
    @ApiAccessLog
    public ResultResp<String> addModule(@RequestBody @Validated InsertModuleReq module) {
        moduleService.save(module);
        return ResultResp.ok();
    }

    @ApiOperation("编辑模块")
    @PostMapping("/module/update")
    @ApiAccessLog
    public ResultResp<String> updateModule(@RequestBody @Validated UpdateModuleReq module) {
        moduleService.update(module);
        return ResultResp.ok();
    }

    @ApiOperation("删除模块")
    @GetMapping("/module/delete")
    @ApiAccessLog
    public ResultResp<String> deleteModule(@RequestParam("id") String id) {
        moduleService.delete(id);
        return ResultResp.ok();
    }

    @ApiOperation("查询模块详情")
    @GetMapping("/module/detail")
    @ApiAccessLog
    public ResultResp<QueryModuleResp> getDetail(@RequestParam("id") String id) {
        QueryModuleResp res = moduleService.getDetail(id);
        return ResultResp.ok(res);
    }

    /**
     * 获取模块列表
     */
    @ApiOperation("获取模块列表")
    @GetMapping("/module/list")
    @ApiAccessLog
    public ResultResp<List<ListQueryModuleResp>> getModuleList(
            @RequestParam(value = "name", required = false)
            @ApiParam("模块名称") String name) {
        List<ListQueryModuleResp> res = moduleService.getModuleList();
        if (StrUtil.isNotEmpty(name)) {
            TreeUtil.filter(res, t -> t.getName().contains(name));
        }
        return ResultResp.ok(res);
    }


    @ApiOperation("排序模块列表")
    @PostMapping("/module/sort")
    @ApiAccessLog
    public ResultResp<String> sort(@RequestBody @Validated SortModuleReq req) {
        moduleService.sort(req);
        return ResultResp.ok();
    }

}
